package com.orangelife.mobile.myhouse.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.curry.android.adapter.FatherAdapter;
import com.curry.orangelife.mobile.R;
import com.common.jump.HouseJumper;
import com.orangelife.mobile.myhouse.biz.ViewTag;
import com.orangelife.mobile.myhouse.entity.HouseBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 房屋列表的适配器
 * <p/>
 * Created by 吴格非 on 2015/11/11.
 */
public class HouseLvAdapter extends FatherAdapter {

    private Context mContext;
    private List<HouseBean> mList = new ArrayList<>();

    public HouseLvAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public BaseAdapter setList(List list) {
        this.mList = list;
        return null;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_house, null);
            holder.tvHouseName = (TextView) convertView.findViewById(R.id.tvHouseName);
            holder.tvFamily = (TextView) convertView.findViewById(R.id.tvFamily);
            holder.tvTenants = (TextView) convertView.findViewById(R.id.tvTenants);
            holder.textView2=(TextView)convertView.findViewById(R.id.textView2);
            holder.tvInvite = (TextView) convertView.findViewById(R.id.tvInvite);
            holder.ivJumpDetailFamily = (ImageView) convertView.findViewById(R.id.ivJumpDetailFamily);
            holder.ivJumpDetailTenants = (ImageView) convertView.findViewById(R.id.ivJumpDetailTenants);
            holder.rlFamily = (RelativeLayout) convertView.findViewById(R.id.rlFamily);
            holder.rlTenant = (RelativeLayout) convertView.findViewById(R.id.rlTenant);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        HouseBean hb = mList.get(position);
        String identityCode = hb.getIdentity();
        String partmentType=hb.getApartmentType();
        showUserView(holder, identityCode,partmentType);
        holder.tvHouseName.setText(hb.getHouseInfo());
        holder.tvInvite.setTag(hb.getLinkAptHouseholderID());
        holder.tvInvite.setOnClickListener(listener);
        holder.rlFamily.setTag(new ViewTag(identityCode, hb.getApartmentID(), hb.getLinkAptHouseholderID()));
        holder.rlTenant.setTag(new ViewTag(identityCode, hb.getApartmentID(), hb.getLinkAptHouseholderID()));
        if(!hb.getFamily().equals("")) {
            holder.rlFamily.setOnClickListener(listener);
        }
        if(!hb.getTenant().equals("")) {
            holder.rlTenant.setOnClickListener(listener);
        }
        handleNoUser(hb.getFamily(), holder.tvFamily, holder.ivJumpDetailFamily);
        handleNoUser(hb.getTenant(), holder.tvTenants, holder.ivJumpDetailTenants);
        return convertView;
    }

    private void showUserView(ViewHolder holder, String identityCode,String partmentType) {
        //apartmentType": "1", --'房屋类型 （ 1车位  住宅2  商铺=3 ）'
        if(partmentType.equals("1")){
            holder.tvInvite.setVisibility(View.GONE);
            holder.rlTenant.setVisibility(View.GONE);
            holder.rlFamily.setVisibility(View.GONE);
            holder.textView2.setText("车位信息");
        }else if(partmentType.equals("2")||partmentType.equals("3")) {
            if (HouseBean.ROLE_TYPE_OWNER_CODE.equals(identityCode)) {
                holder.tvInvite.setVisibility(View.VISIBLE);
                holder.rlTenant.setVisibility(View.VISIBLE);
                holder.rlFamily.setVisibility(View.VISIBLE);
                holder.textView2.setText("房屋信息");
            } else if (HouseBean.ROLE_TYPE_FAMILY_CODE.equals(identityCode)) {
                holder.tvInvite.setVisibility(View.GONE);
                holder.rlTenant.setVisibility(View.GONE);
                holder.rlFamily.setVisibility(View.VISIBLE);
            } else if (HouseBean.ROLE_TYPE_TENANT_CODE.equals(identityCode)) {
                holder.tvInvite.setVisibility(View.GONE);
                holder.rlTenant.setVisibility(View.VISIBLE);
                holder.rlFamily.setVisibility(View.GONE);
                holder.textView2.setText("房屋信息");
            }
        }
    }

    class ViewHolder {
        /*房屋信息*/
        TextView tvHouseName;
        /*亲属信息列表*/
        TextView tvFamily;
        /*租客信息列表*/
        TextView tvTenants;
        /*邀请*/
        TextView tvInvite;
        TextView textView2;
        /*进入详情页*/
        ImageView ivJumpDetailFamily;
        ImageView ivJumpDetailTenants;
        RelativeLayout rlFamily;
        RelativeLayout rlTenant;
    }

    /* 如果无人，则隐藏邀请 */
    private void handleNoUser(String usernames, TextView tvNames, View ivJumpDetail) {
        if ("".equals(usernames)) {
            tvNames.setText("尚未添加");
            ivJumpDetail.setVisibility(View.INVISIBLE);
            ivJumpDetail.setOnClickListener(null);
        } else {
            tvNames.setText(usernames);
            ivJumpDetail.setVisibility(View.VISIBLE);
            ivJumpDetail.setOnClickListener(listener);
        }
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tvInvite:
                    HouseJumper.jumpToInvite(mContext, String.valueOf(v.getTag()));
                    break;
                case R.id.rlFamily:
                    jumpToFamilyOrTenant(v, true);
                    break;
                case R.id.rlTenant:
                    jumpToFamilyOrTenant(v, false);
                    break;
            }

        }
    };

    private void jumpToFamilyOrTenant(View v, boolean isFamily) {
        ArrayList<String> list = new ArrayList<>();
        String identityCode = ((ViewTag) v.getTag()).getValue(0);/*身份*/
        if (HouseBean.ROLE_TYPE_OWNER_CODE.equals(identityCode)) {/*如果是户主，跳转到列表*/
            if (isFamily) {/*（1）是家属还是租客*/
                list.add(HouseBean.ROLE_TYPE_FAMILY_CODE);
            } else {
                list.add(HouseBean.ROLE_TYPE_TENANT_CODE);
            }
            String apartmentID = ((ViewTag) v.getTag()).getValue(1);/*（2）房号ID*/
            list.add(apartmentID);
            HouseJumper.jumpToResidentList(mContext, list);
        } else {/*如果是租客，直接跳转到详情*/
            String applicationID = ((ViewTag) v.getTag()).getValue(2);/*（2）认证ID*/
            list.add(applicationID);
            HouseJumper.jumpToResidentDetail(mContext, list);
        }
    }
}
